/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import Entidades.Alunos;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Roney
 */
public class exemplosHibernate {
    
    public void inserirAluno(){
        try{
            SessionFactory sesfac = new Configuration().configure().buildSessionFactory();
            Session ses = sesfac.openSession();
            
            Alunos alu = new Alunos();
            alu.setAluCodigo(1);
            alu.setAluNome("Roney");
            alu.setAluCidade("Pirassununga");
            alu.setAluCurso("Info");
            alu.setAluFone("35625669");
            
            Transaction tx = ses.beginTransaction();
            ses.save(alu);
            tx.commit();
            ses.close();
        }
        catch (Exception ex){
            
            JOptionPane.showMessageDialog(null,"Erro ao inserir aluno "+ ex);
        }
    }
    
    public void atualizaAluno(){
        try{
            SessionFactory sesfac = new Configuration().configure().buildSessionFactory();
            Session ses = sesfac.openSession();
            
            Alunos alu = new Alunos();
            alu.setAluCodigo(1);
            alu.setAluNome("Barrabin");
            alu.setAluCidade("Pirassununga");
            alu.setAluCurso("Info");
            alu.setAluFone("35625669");
            
            Transaction tx = ses.beginTransaction();
            ses.update(alu);
            tx.commit();
            ses.close();
        }
        catch (Exception ex){
            
            JOptionPane.showMessageDialog(null,"Erro ao inserir aluno "+ ex);
        }
    }
    
    public void excluiAluno(){
        try{
            SessionFactory sesfac = new Configuration().configure().buildSessionFactory();
            Session ses = sesfac.openSession();
            
            Alunos alu = new Alunos();
            alu.setAluCodigo(1);
                        
            Transaction tx = ses.beginTransaction();
            ses.delete(alu);
            tx.commit();
            ses.close();
        }
        catch (Exception ex){
            
            JOptionPane.showMessageDialog(null,"Erro ao inserir aluno "+ ex);
        }
    }
    
    public void listaAluno(){
        try{
            SessionFactory sesfac = new Configuration().configure().buildSessionFactory();
            Session ses = sesfac.openSession();
            
            List<Alunos> lista_aluno = new ArrayList();
            lista_aluno = ses.createQuery("from Alunos").list();
            int tamanho_lista = lista_aluno.size();
            
            for (int i=0;i<tamanho_lista;i++){
                Alunos alu = lista_aluno.get(i);
                System.out.println(alu.getAluCodigo()+" - "+alu.getAluNome()+" - "+alu.getAluCidade()+" - "+alu.getAluCurso()+" - "+alu.getAluFone());
            }
            
            ses.close();
        }
        catch (Exception ex){
            
            JOptionPane.showMessageDialog(null,"Erro ao inserir aluno "+ ex);
        }
    }
    
}
